package com.syyo.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.product.domain.entity.AttrAttrgroupRelationEntity;
import com.syyo.gulimall.product.domain.req.AttrAttrgroupRelationReq;
import com.syyo.gulimall.product.mapper.AttrAttrgroupRelationMapper;
import com.syyo.gulimall.product.service.AttrAttrgroupRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 属性&属性分组关联
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:32:12
 */
@Service
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationMapper, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationMapper attrAttrgroupRelationMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public boolean add(AttrAttrgroupRelationReq req) {
        List<Long> attrIds = req.getAttrIds();
        Long attrGroupId = req.getAttrGroupId();
        List<AttrAttrgroupRelationEntity> collect = attrIds.stream().map(item -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelation.setAttrId(item);
            attrAttrgroupRelation.setAttrGroupId(attrGroupId);
            return attrAttrgroupRelation;
        }).collect(Collectors.toList());
        boolean b = this.saveBatch(collect);
        return b;

//        AttrAttrgroupRelationEntity attrAttrgroupRelation = new AttrAttrgroupRelationEntity();
//        // 添加创建时间和更新时间
//        int insert = attrAttrgroupRelationMapper.insert(attrAttrgroupRelation);
//        if (insert != 1){
//            throw new SysException(ResultEnum.E_10001.getCode(),ResultEnum.E_10001.getMessage());
//        }
//        return ResultUtils.ok(insert);
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = attrAttrgroupRelationMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(AttrAttrgroupRelationReq req) {
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = attrAttrgroupRelationMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public AttrAttrgroupRelationEntity findOne(Integer id) {
        return attrAttrgroupRelationMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<AttrAttrgroupRelationEntity> findAll(Integer pageNum, Integer pageSize, AttrAttrgroupRelationReq req) {
        Page<AttrAttrgroupRelationEntity> teacherPage = new Page<AttrAttrgroupRelationEntity>(pageNum, pageSize);
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        return attrAttrgroupRelationMapper.selectPage(teacherPage, wrapper);
    }

}